<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品类型大类</title>
    <script src="js/axios.min.js"></script>
    <script src="js/vue.min.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="ele-2.15.7/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="ele-2.15.7/index.js"></script>
</head>

<style>

    .text {
        font-size: 14px;
    }

    .item {
        padding: 18px 0;
    }

    .box-card {
        width: 100%;
        height: 200px;
        margin: auto;
        margin-top:30px ;
    }

    .btn-add {
        float: right;
    }



    .btn-add{
        font-size: 14px;

    }
    .operate-container{
        height: 59px;
    }

    /*.pagerstyle{*/
    /*    margin-left: 1247px;*/
    /*}*/

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

    .container-filter{
        height: 140px;
    }

    .operate-container{
        height: 60px;
        margin-bottom:20px;
    }


</style>

<body>

<div id="app">


    <el-card class="box-card container-filter" shadow="never">
        <div class="search-top">
            <i class="el-icon-search"></i>
            <span> 筛选搜索</span>
            <el-button style="float: right;" class="el-button--primary" @click="query(1)">查询结果</el-button>
            <el-button style="float: right; margin-right: 15px">重置</el-button>
        </div>

        <div>
            <el-form :inline="true" class="demo-form-inline"
                     style="display: flex; align-items: center">

                <div>
                    <el-form-item label="输入搜索"  >
                        <el-input v-model="tname" placeholder="商品类别名称" style="padding-right: 0px;width: 219px"></el-input>
                    </el-form-item>
                </div>

                <div>
                    <el-form-item label="商品状态"  >
                        <el-select v-model="status" placeholder="请选择">
                            <el-option
                                    v-for="item in statusOption"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </div>

            </el-form>
        </div>

    </el-card>

    <el-card class="box-card operate-container " shadow="never">
        <i class="el-icon-tickets"></i>
        <span>数据列表</span>
        <el-button class="btn-add" size="mini" @click="create" >
            <span>添加</span>
        </el-button>

    </el-card>

    <el-table
            :header-cell-style="{'text-align':'center'}"
            :cell-style="{'text-align':'center'}"
            stripe
              class="el-table--border  "
              :data="tableData.data">
        <el-table-column prop="tid" label="编号"></el-table-column>
        <el-table-column prop="tname" label="类型名称"></el-table-column>
        <el-table-column prop="" label="状态" width="130px">
            <!--            商品上架和推荐  加入插槽后开关就能移动了-->
            <template slot-scope="scope">
                <el-switch
                        v-model="scope.row.status"
                        inactive-text="上架:"
                        active-color="#409EFF"
                        inactive-color="#ff4949"
                        :active-value="shoppingActive"
                        :inactive-value="shoppingInactive"
                        @change="changeStatus(scope.row)"
                        inactive-color = "red">
                </el-switch>
            </template>
        </el-table-column>

<!--        <el-table-column prop="total" label="商品销量"></el-table-column>-->

        <el-table-column label="操作">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        @click="handleEdit(scope.$index, scope.row);">编辑
                </el-button>

                <el-button
                        size="mini"
                        type="danger"
                        @click="remove(scope.row)">删除
                </el-button>

                <el-button
                        size="mini"
                        @click="">查看包含类别
                </el-button>

            </template>
        </el-table-column>

    </el-table>

    <el-footer style="text-align: right">
        <el-pagination :page-size="pageSize" @current-change="query" @size-change="sizeChange"
                       background :page-sizes="[10,15,20]"
                       layout="sizes, prev, pager, next"
                       :total="tableData.total"
                       :current-page.sync="tableData.page">
        </el-pagination>
    </el-footer>

    <el-dialog title="商品类型增改" :visible.sync="dialogFormVisible">
        <div style="display: flex; justify-content: space-around;">
            <el-form>
                <el-form-item label="商品类型名称" :label-width="formLabelWidth">
                    <el-input v-model="editData.tname" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="saveProductType">确 定</el-button>
        </div>

    </el-dialog>
</div>

<script>
    var vue = new Vue({
        el: "#app",
        data: {
            tname: "",
            status: "",
            pageSize : 10,
            shoppingActive : "1",
            shoppingInactive : "0",
            dialogFormVisible: false,
            formLabelWidth: "100px",
            editData: {tid: "", tname: "", status: ""},
            //表格数据
            tableData: {page:1},
            // 状态下拉框
            statusOption: [{
                value: '',
                label: '全部'
            }, {
                value: '1',
                label: '上架'
            }, {
                value: '0',
                label: '下架'
            }],
        },
        created(){
            this.query(1);
        },
        methods: {
            createStateFilter(queryString) {
                return (state) => {
                    return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
                };
            },
            handleSelect(item) {
                console.log(item);
            },

            handleEdit(index, data) {
                // 修改
                this.create();
                for (let i in data){
                    this.editData[i] = data[i];
                }
                this.dialogFormVisible = true;
            },
            handleDelete(index, row) {
                console.log(index, row);
            },
            query(page) {
                axios.get("QueryProductTypeServlet.s",{
                    params: {
                        tname : this.tname,
                        status : this.status,
                        currentPage : page, //当前页数
                        pageSize: this.pageSize //每页显示条目个数
                    }
                }).then(res=>{
                    this.tableData = res.data;
                    // for (let d in this.tableData) {
                    //     if (d.status == 1){
                    //         this.tableData.status = true;
                    //     } else{
                    //         this.tableData.status = false;
                    //     }
                    // }
                    this.tableData.page = page;
                })
            },
            sizeChange(page){
                this.pageSize =page;
                this.query(1);
            },
            create(){
                this.editData = {tid: "", tname: "", status: ""},
                this.dialogFormVisible = true;
            },
            // 添加
            saveProductType(){
                // 构建post参数
                var params = new URLSearchParams();

                for (let name in this.editData){
                    params.append(name, this.editData[name]);
                }

                // 发送 ajax post 请求
                axios.post("SaveProductTypeServlet.s", params).then(res=>{
                    if (res.data.code == 1){
                        this.$message(res.data.msg);
                        this.query(1); // 刷新表格
                        this.dialogFormVisible = false; // 关闭弹窗
                    } else{
                        this.$alert(res.data.msg);
                    }
                })
            },

            // 物理删除
            remove(data){
                this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
                    type: 'warning'
                }).then(() => {
                    axios.get("RemoveProductTypeMaxServlet.s", {
                        params: {
                            id: data.tid,
                            flag: "物理删除"
                        }
                    }).then(res =>{
                        this.$message(res.data.msg);
                        this.query(1);
                    })
                }).catch( ()=> {
                    this.$message({
                        type: 'info',
                        message:"已取消删除!"
                    });
                });
            },

            // 逻辑删除
            changeStatus(row){

                var url = "RemoveProductTypeMaxServlet.s";
                axios.get(url,{
                    params : {
                        id : row.tid,
                        status : row.status,
                        flag : "逻辑删除"
                    }
                }).then(res=>{
                    console.info(res.data.msg);
                    this.$message(res.data.msg);
                });
            },

        }
    })
</script>

</body>
</html>


